/*
 * (C) Copyright 2013 George Daswani (georgedaswani@gmail.com) and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-2.1.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *    George Daswani (georgedaswani@gmail.com)
 */

package am.internal;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import com.sun.jna.Native;

public class AMStaticLib {

	public final static AMLibrary INSTANCE;

	static {

		InputStream configStream = null;

		String configFilePath = "/am/internal/AMLibrary.properties";

		try {
			Properties properties = new Properties();

			configStream = AMStaticLib.class
					.getResourceAsStream(configFilePath);

			if (configStream != null) {
				properties.load(configStream);

				String apiLib = properties.getProperty("AMAPI_LIB");

				if (false == properties.isEmpty()) {

					System.loadLibrary(apiLib);

					INSTANCE = (AMLibrary) Native.loadLibrary(apiLib,
							AMLibrary.class);
				} else {
					throw new IllegalStateException(String.format(
							"properties file [%s] is empty", configFilePath));
				}
			} else {
				throw new IllegalStateException(String.format(
						"could not load properties file [%s]", configFilePath));
			}

		} catch (UnsatisfiedLinkError error) {
			throw new IllegalStateException(String.format(
					"Could not load Asset Manager native library: %s",
					error.getMessage()));
		} catch (IOException ioe) {
			throw new IllegalStateException(String.format(
					"Could not load Asset Manager native library: %s",
					ioe.getMessage()));
		}

	}

}
